/*
 * #%L
 * Osm2garminAPI
 * %%
 * Copyright (C) 2011 Frantisek Mantlik <frantisek at mantlik.cz>
 * %%
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as
 * published by the Free Software Foundation, either version 2 of the 
 * License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public 
 * License along with this program.  If not, see
 * <http://www.gnu.org/licenses/gpl-2.0.html>.
 * #L%
 */
package be.dch.srtm2osm.domain;

import java.io.PrintStream;
import java.util.ArrayList;
import java.util.List;

/**
 * 
 * @author fm
 */
public class Contour {

    public final transient List<Point> data = new ArrayList<Point>();
    public double z;
    public boolean finished = false;
    public boolean closed = false;

    public boolean isClosed() {
	return this.closed;
    }

    public void setClosed(final boolean closed) {
	this.closed = closed;
    }

    public List<Point> getData() {
	return this.data;
    }

    public boolean isFinished() {
	return this.finished;
    }

    public void setFinished(final boolean finished) {
	this.finished = finished;
    }

    public double getZ() {
	return this.z;
    }

    public void setZ(final double z) {
	this.z = z;
    }

    public void outputOsm(final long wayid, final long startid, final boolean major, final PrintStream s, final boolean nodes, final boolean way) {
	int size = this.data.size();
	if (this.isClosed()) {
	    size--; // repeat first node at the way end
	}
	if (this.data.isEmpty() || (size < 2)) {
	    return;
	}
	if (nodes) {
	    long id = startid;
	    for (int i = 0; i < size; i++) {
		s.println("<node id=\"" + id + "\" lat=\"" + this.data.get(i).getX() + "\" lon=\"" + this.data.get(i).getY() + "\" />");
		id++;
	    }
	}
	if (way) {
	    long id = startid;
	    s.println("<way id=\"" + wayid + "\">");
	    for (int i = 0; i < size; i++) {
		s.println("<nd ref=\"" + id + "\" />");
		id++;
	    }
	    if (this.isClosed()) { // last node equals to the first one
		s.println("<nd ref=\"" + startid + "\" />");
	    }
	    s.println("<tag k=\"ele\" v=\"" + this.z + "\" />");
	    s.println("<tag k=\"contour\" v=\"elevation\" />");
	    if (major) {
		s.println("<tag k=\"contour_ext\" v=\"elevation_major\" />");
	    } else {
		s.println("<tag k=\"contour_ext\" v=\"elevation_minor\" />");
	    }
	    s.println("</way>");
	}
    }
}
